package com.yimixian.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.yimixian.app.R;
import com.yimixian.app.cart.GoodsItemPresenter;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.ui.HeightAutoAdjustableImageView;
import com.yimixian.app.ui.SessionItemView;
import com.yimixian.app.util.UiUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhilian-2 on 2015/10/16.
 */
public class SessionAdapter extends BaseAdapter{

    LayoutInflater mInflater;
    List<GoodsItem> mList = new ArrayList<>();
    Context context;
    private DataManager mDataManager;
    private GoodsItemPresenter.OnPlusButtonClickListener mOnPlusButtonClickListener;

    public SessionAdapter(Context context, List<GoodsItem> mList,
                          GoodsItemPresenter.OnPlusButtonClickListener plusButtonClickListener){
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.mList = mList;
        if (mList == null) this.mList = new ArrayList<>();
        mOnPlusButtonClickListener = plusButtonClickListener;
        mDataManager = DataManager.getInstance();
    }

    public void setList(List<GoodsItem> mList){
        this.mList = mList;
        if (mList == null) this.mList = new ArrayList<>();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public GoodsItem getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = new SessionItemView(context);
            convertView.setLayoutParams(new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        SessionItemView sessionItemView = (SessionItemView) convertView;
        sessionItemView.setOnPlusButtonClickListener(mOnPlusButtonClickListener);
        sessionItemView.bindData(getItem(position));
        return sessionItemView;
    }
}
